package com.weavernorth.action.purchase;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

public class PurchaseApprove implements Action {
    public static Log log = LogFactory.getLog(PurchaseApprove.class.getName());

    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet rsTwo = new RecordSet();
        RecordSet rsThree = new RecordSet();
        RecordSet rsFour = new RecordSet();

        //获取采购类别为补充的值
        String value = Util.null2String(bb.getPropValue("purchaseurl", "value"));
        log.info("配置文件中补充的值:" + value);
        //获取采购类别为常规的值
        String valuet = Util.null2String(bb.getPropValue("purchaseurl", "valuet"));
        //请求ID
        String requestid = requestInfo.getRequestid();
        //表单名称
        String tablename = requestInfo.getRequestManager().getBillTableName();
        log.info("测试表名:" + tablename);
        //查询采购类别为补充的sql,根据人判断条数
        String sql = "select fm.cglx，fm.sqsj ,fm.sqr ,fm.fqgc from " + tablename + " fm where   fm.requestid = '" + requestid + "'";
        log.info("查询采购类别值的sql" + sql);
        rs.execute(sql);
        //如果有值
        if (rs.next()) {
            //采购类型
            String strCglx = Util.null2String(rs.getString("cglx"));
            //申请人
            String strCreater = Util.null2String(rs.getString("sqr"));
            //获取表单日期
            String strDays = Util.null2String(rs.getString("sqsj"));
            //工厂
            String strFqgc=Util.null2String(rs.getString("fqgc"));
            log.info("申请时间:" + strDays);
            int intDay = 0;
            //获取日期的天数并转化为int类型，为比较做准备
            if (!"".equals(strDays)) {
                String[] dayStr = strDays.split("-");
                intDay = Integer.parseInt(dayStr[2]);
                log.info("天==：" + intDay);
            }

            //查询明细表数量
            String sqlCount = "select count(1) num from " + tablename + "_dt1 d," + tablename + " t  where t.id=d.mainid  and t.requestid= '" + requestid + "'";
            //查询明细表物资类别的sql
            String sqlTwo = sqlCount + " and  （d.wzlb not like '%辅料-包装物%'  and  d.wzlb  not like '%辅料-服装服饰辅料%'  and  d.wzlb  not like '%辅料-材辅料%')";
            log.info("查询明细表物资类别的sql" + sqlTwo);
            log.info("查询明细表数量的sql" + sqlCount);
            rsTwo.execute(sqlTwo);
            //不全是 sql中的物资类别,需要控制流程提交
            if (rsTwo.next()) {
                int intNum = Integer.parseInt(rsTwo.getString("num"));
                log.info("当前num值:"+intNum);

                if (intNum>0) {
                    //判断此创建人此申请时间的月份是否提交过的sql
                    String sqlCheck = "select 1 from " + tablename + " fm,"+tablename+"_dt1 d ,workflow_requestbase wr where fm.id=d.mainid and  fm.requestid = wr.requestid  and wr.currentnodetype!=0 and fm.cglx='" + strCglx + "' and fm.sqr='" + strCreater + "' and substr(fm.sqsj,0,7)=(select substr(sqsj,0,7) from " + tablename + " where requestid='" + requestid + "')";
                    //明细表全是这三个类别的,则不算次数
                     sqlCheck = sqlCheck + " and fqgc='"+strFqgc+"' and  （d.wzlb not like '%辅料-包装物%'  and  d.wzlb  not like '%辅料-服装服饰辅料%'  and  d.wzlb  not like '%辅料-材辅料%')";
                    log.info("判断每月是否提交过的sql:" + sqlCheck);
                    rsThree.execute(sqlCheck);
                    //流程中的采购类型为  时
                    if (strCglx.equals(value)) {
                        //获取允许提交的最小最大日期并转为int类型
                        int minData = Integer.parseInt(Util.null2String(bb.getPropValue("purchaseurl", "mindata")));
                        log.info("补充的最小日期" + minData);
                        int maxData = Integer.parseInt(Util.null2String(bb.getPropValue("purchaseurl", "maxdata")));
                        log.info("补充的最大日期" + maxData);
                        //在5到10日内是否提交过
                        if (intDay >= minData && intDay <= maxData && rsThree.getCounts() > 0) {
                            //获取错误信息
                            String propValue = Util.null2String(bb.getPropValue("purchaseurl", "tso"));
                            log.info("错误信息1" + propValue);
                            //提示错误信息
                            requestInfo.getRequestManager().setMessagecontent(propValue);
                            return FAILURE_AND_CONTINUE;//失败
                        } else if((intDay < minData || intDay > maxData)){
                            //获取错误信息2
                            String propValueTwo = Util.null2String(bb.getPropValue("purchaseurl", "tst"));
                            bb.writeLog("错误信息2" + propValueTwo);
                            //提示错误信息
                            requestInfo.getRequestManager().setMessagecontent(propValueTwo);
                            return FAILURE_AND_CONTINUE;//失败
                        }
                    }
                    //采购类型为 补充 时
                    else if (strCglx.equals(valuet)) {
                        //执行查询明细表总条数
                        rsFour.executeQuery(sqlCount);
                        rsFour.next();
                        //明细表的数据条数

                        bb.writeLog("明细表数据的条数：" + intNum);
                        //获取允许提交的最小最大日期并转为int类型
                        int dataMin = Integer.parseInt(Util.null2String(bb.getPropValue("purchaseurl", "datamin")));
                        bb.writeLog("常规的最小日期" + dataMin);
                        int dataMax = Integer.parseInt(Util.null2String(bb.getPropValue("purchaseurl", "datamax")));
                        bb.writeLog("常规的最大日期" + dataMax);

                        //在18到23日内是否提交过
                        if (intDay >= dataMin && intDay <= dataMax && intNum > 10 ) {
                            //获取错误信息4
                            String propValue = Util.null2String(bb.getPropValue("purchaseurl", "tsf"));
                            bb.writeLog("错误信息4" + propValue);
                            //提示错误信息
                            requestInfo.getRequestManager().setMessagecontent(propValue);
                            return FAILURE_AND_CONTINUE;//失败
                        } else if ((intDay < dataMin || intDay > dataMax)) {
                            //获取错误信息5
                            String propValueTwo = Util.null2String(bb.getPropValue("purchaseurl", "tsti"));
                            bb.writeLog("错误信息5" + propValueTwo);
                            //提示错误信息
                            requestInfo.getRequestManager().setMessagecontent(propValueTwo);
                            return FAILURE_AND_CONTINUE;//失败
                        } else if (intDay >= dataMin && intDay <= dataMax && rsThree.getCounts() > 0 && intNum < 10) {
                            //获取错误信息3
                            String propValueThree = Util.null2String(bb.getPropValue("purchaseurl", "t"));
                            bb.writeLog("错误信息3" + propValueThree);
                            //提示错误信息
                            requestInfo.getRequestManager().setMessagecontent(propValueThree);
                            return FAILURE_AND_CONTINUE;//失败
                        }


                    }
                }
            }

        }
        bb.writeLog("逻辑执行结束");
        return SUCCESS;
    }
}

